#include "hyperspin.h"

#include <QXmlSimpleReader>
#include <QXmlInputSource>
#include <QXmlDefaultHandler>
#include <QFileInfo>
#include <QApplication>
#include <QDebug>

namespace hs
{
QString ROOT_FOLDER = "";
QString WAV_UP = "";
QString WAV_DOWN = "";
QString WAV_LEFT = "";
QString WAV_RIGHT = "";
QString WAV_ENTER = "";
QString WAV_BACK = "";
QString BG_SYSTEMS = "";
QString BG_GAMES = "";
const QString MAIN_MENU = "Main Menu";
}

class HyperspinContentHandler: public QXmlDefaultHandler
{
  QVector<hs::Entry> _result;
  hs::Entry _currentEntry;
  bool _parseDescription;
public:
  QVector<hs::Entry> result() const { return _result; }

  HyperspinContentHandler()
    : _parseDescription(false)
  {
  }

  bool startElement(const QString &, const QString &, const QString &qName, const QXmlAttributes &atts)
  {
    if (qName == "game")
    {
      _currentEntry.first = atts.value(QString("name"));
      _currentEntry.second= _currentEntry.first;
    }
    else if (qName == "description")
    {
      _parseDescription = true;
    }
    return true;
  }

  bool characters(const QString &ch)
  {
    if (_parseDescription)
    {
      _currentEntry.second = ch;
    }
    return true;
  }

  bool endElement(const QString &, const QString &, const QString &qName)
  {
    if (qName == "game")
    {
      _result.push_back(_currentEntry);
    }
    else if (qName == "description")
    {
      _parseDescription = false;
    }
    return true;
  }
};

QString dbFile(const QString &system)
{
  return (hs::ROOT_FOLDER + "/databases/%1/%1.xml").arg(system);
}

QVector<hs::Entry> hs::entries(const QString &system)
{
  HyperspinContentHandler contentHandler;
  {
    QXmlSimpleReader reader;
    QFile sourceFile(dbFile(system));
    QXmlInputSource source(&sourceFile);
    reader.setContentHandler(&contentHandler);
    reader.setErrorHandler(&contentHandler);
    reader.parse(&source);
  }

  return contentHandler.result();
}


QString hs::art(const QString &system, const QString &name)
{
  QString video;
  foreach(const QString& ext, QStringList() << "flv" << "mp4")
  {
    video = (hs::ROOT_FOLDER + "/videos/%1/%2.%3").arg(system).arg(name).arg(ext);
    if (QFileInfo(video).exists())
      return video;
  }
  return hs::ROOT_FOLDER + "/videos/No Video.flv";
}


QString hs::systemArt(const QString &system)
{
  return hs::art(hs::MAIN_MENU,system);
}

void hs::init_dirs(const QString& root)
{
  hs::ROOT_FOLDER = root;
  hs::WAV_UP = ROOT_FOLDER + "\\sounds\\up.wav";
  hs::WAV_DOWN = ROOT_FOLDER + "\\sounds\\down.wav";
  hs::WAV_LEFT = ROOT_FOLDER + "\\sounds\\left.wav";
  hs::WAV_RIGHT = ROOT_FOLDER + "\\sounds\\right.wav";
  hs::WAV_ENTER = ROOT_FOLDER + "\\sounds\\enter.wav";
  hs::WAV_BACK = ROOT_FOLDER + "\\sounds\\back.wav";
  hs::BG_SYSTEMS = ROOT_FOLDER + "\\backgrounds\\systems.jpg";
  hs::BG_GAMES = ROOT_FOLDER + "\\backgrounds\\games.jpg";
}
